package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConst;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.exception.BusinessRuntimeException;
import com.itheima.pojo.Permission;
import com.itheima.service.PermissionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @description:
 * @author: LuoXuhuan
 * @date: 2020/10/29-0:07
 * @Version: 1.0.0
 */
@RestController
@Slf4j
@RequestMapping("/permission")
public class PermissionController {

    @Reference
    PermissionService permissionService;

    /**
     * 添加权限
     *
     * @param permission
     * @return
     */
    @RequestMapping("/addPermission")
    public Result addPermission(@RequestBody Permission permission) {

        permissionService.addPermission(permission);

        return new Result(true, "权限添加成功");
    }

    /**
     * 分页查询权限
     *
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean) {

        PageResult page = permissionService.findPage(queryPageBean);

        return new Result(true, "分页查询权限成功", page);
    }

    /**
     * 根据权限id查询权限信息
     *
     * @param id
     * @return
     */
    @RequestMapping("/findById")
    public Result findById(Integer id) {

        Permission permission = permissionService.findById(id);

        return new Result(true, "权限查询成功", permission);
    }

    /**
     * 更新权限信息
     *
     * @param permission
     * @return
     */
    @RequestMapping("/edit")
    public Result Edit(@RequestBody Permission permission) {

        permissionService.edit(permission);

        return new Result(true, "权限更新成功");
    }

    /**
     * 通过id删除权限
     *
     * @param id
     * @return
     */
    @RequestMapping("/delById")
    public Result delById(Integer id) {

        Integer rolePerm_role = permissionService.findRolePerm_Role(id);
        if (rolePerm_role > 0)
            throw new BusinessRuntimeException("角色表关联该权限，暂无法删除");
        permissionService.delById(id);
        return new Result(true, "删除权限成功");
    }

    @RequestMapping("/findAll")
    public Result findAll(){
        log.debug("查询所有的权限");
       List<Permission> permissionList =permissionService.findAll();

       log.debug("查询所有的权限:"+permissionList);

       return new Result(true, MessageConst.ACTION_SUCCESS,permissionList);
    }

}
